﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKArch.AreaSet;
using YJKArch.AreaStatistical.SearchLoop;
using YJKArchKernel.OutWallSearch;
using YJKRGeometry;
using YJKRGeometry.CurveUtility;
using HYView;
using TopologySearch;

namespace YJKArch.ModifiedArea
{
	public class OffsetWallLine
	{
		public OffsetWallLine(ExternalCommandData cmdData, string inputLocation)
		{
			this.m_Revit = cmdData;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_strLineLocation = inputLocation;
		}

		public XYZ GetAreaCurveCenterPt(List<Curve> lstCurves)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = new XYZ();
			foreach (Curve curve in lstCurves)
			{
				if (curve != null)
				{
					XYZ endPoint = curve.GetEndPoint(0);
					XYZ endPoint2 = curve.GetEndPoint(1);
					list.Add(new XYZ(endPoint.X, endPoint.Y, endPoint.Z));
					list.Add(new XYZ(endPoint2.X, endPoint2.Y, endPoint.Z));
				}
			}
			foreach (XYZ xyz2 in list)
			{
				xyz += xyz2;
			}
			xyz = new XYZ(xyz.X / (double)list.Count, xyz.Y / (double)list.Count, xyz.Z / (double)list.Count);
			return xyz;
		}

		public XYZ Centroid(List<Curve> curveArray)
		{
			bool flag = false;
			List<XYZ> list = new List<XYZ>();
			foreach (Curve curve in curveArray)
			{
				if (curve != null)
				{
					IList<XYZ> list2 = curve.Tessellate();
					if (list2 != null)
					{
						for (int i = 0; i < list2.Count - 1; i++)
						{
							if (list2[i].Z != 0.0)
							{
								flag = true;
								list.Add(new XYZ(list2[i].X, list2[i].Y, 0.0));
							}
						}
					}
				}
			}
			if (!flag)
			{
				return this.GetAreaCurveCenterPt(curveArray);
			}
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			int count = list.Count;
			for (int j = 0; j < count - 1; j++)
			{
				num += (list[j].X * list[j + 1].Y - list[j + 1].X * list[j].Y) / 2.0;
				num2 += (list[j].X * list[j + 1].Y - list[j + 1].X * list[j].Y) * (list[j].X + list[j + 1].X);
				num3 += (list[j].X * list[j + 1].Y - list[j + 1].X * list[j].Y) * (list[j].Y + list[j + 1].Y);
			}
			num += (list[count - 1].X * list[0].Y - list[0].X * list[count - 1].Y) / 2.0;
			num2 += (list[count - 1].X * list[0].Y - list[0].X * list[count - 1].Y) * (list[count - 1].X + list[0].X);
			num3 += (list[count - 1].X * list[0].Y - list[0].X * list[count - 1].Y) * (list[count - 1].Y + list[0].Y);
			num2 /= 6.0 * num;
			num3 /= 6.0 * num;
			return new XYZ(num2, num3, 0.0);
		}

		public static void RemoveRepeatCurves(List<Curve> curves)
		{
			for (int i = 0; i < curves.Count; i++)
			{
				if (!(curves[i] == null))
				{
					for (int j = i + 1; j < curves.Count; j++)
					{
						if (!(curves[j] == null))
						{
							SetComparisonResult setComparisonResult = curves[i].Intersect(curves[j]);
							if (64 == (int)setComparisonResult)
							{
								curves.RemoveAt(j);
								j--;
							}
						}
					}
				}
			}
		}

		public List<Curve> AddCurveToUseCurves(List<Curve> lstUseCurves)
		{
			OffsetWallLine.RemoveRepeatCurves(lstUseCurves);
			new List<Curve>();
			List<XYZ> list = new List<XYZ>();
			for (int i = 0; i < lstUseCurves.Count; i++)
			{
				if (lstUseCurves[i] != null)
				{
					XYZ endPoint = lstUseCurves[i].GetEndPoint(0);
					XYZ endPoint2 = lstUseCurves[i].GetEndPoint(1);
					list.Add(new XYZ(endPoint.X, endPoint.Y, 0.0));
					list.Add(new XYZ(endPoint2.X, endPoint2.Y, 0.0));
				}
			}
			return this.FindLinkeCurves(list);
		}

		private void SortCurves(ref List<Curve> curves)
		{
			List<Curve> list = new List<Curve>();
			list.Add(curves.First<Curve>());
			XYZ endPoint = curves.First<Curve>().GetEndPoint(1);
			curves.RemoveAt(0);
			int num = 0;
			while (curves.Count > 0)
			{
				num++;
				num %= curves.Count;
				if (endPoint.IsAlmostEqualTo(curves[num].GetEndPoint(0), 0.1))
				{
					list.Add(curves[num]);
					endPoint = curves[num].GetEndPoint(1);
					curves.RemoveAt(num);
					num--;
				}
				else if (endPoint.IsAlmostEqualTo(curves[num].GetEndPoint(1), 0.1))
				{
					endPoint = curves[num].GetEndPoint(0);
					list.Add(curves[num].CreateReversed());
					curves.RemoveAt(num);
					num--;
				}
			}
			curves.Clear();
			curves.AddRange(list);
		}

		public List<Pole2D> GetOffsetCurves(List<Dictionary<Wall, Curve>> dicWall2Curve, List<Wall> lstAllOutWall, string RangeType)
		{
			List<Pole2D> list = new List<Pole2D>();
			new List<Pole2D>();
			new List<Curve>();
			List<int> list2 = new List<int>();
			foreach (Wall wall in lstAllOutWall)
			{
				list2.Add(wall.Id.IntegerValue);
			}
			foreach (Dictionary<Wall, Curve> dictionary in dicWall2Curve)
			{
				foreach (KeyValuePair<Wall, Curve> kvp in dictionary)
				{
					Pole2D wallInCurve = this.GetWallInCurve(kvp, list2);
					list.Add(wallInCurve);
				}
			}
			new StructureLoopSearcher(this.m_Revit).FormatDoubleLine(ref list);
			return this.OffsetCurveByRangeType(list, RangeType, list2);
		}

		public List<Wall> GetOutWallsCurViewPlan(ViewPlan curViewPlan, Autodesk.Revit.DB.Document doc)
		{
			List<Wall> result = new List<Wall>();
			OutWallFilter.Search(doc, curViewPlan.Id, ref result, true);
			return result;
		}

		public List<Wall> GetSelectWalls(List<Room> lstRooms)
		{
			List<Wall> list = new List<Wall>();
			List<int> list2 = new List<int>();
			foreach (SpatialElement spatialElement in lstRooms)
			{
				SpatialElementBoundaryOptions spatialElementBoundaryOptions = new SpatialElementBoundaryOptions();
				IEnumerable<IList<BoundarySegment>> boundarySegments = spatialElement.GetBoundarySegments(spatialElementBoundaryOptions);
				new List<Pole>();
				foreach (IList<BoundarySegment> list3 in boundarySegments)
				{
					foreach (BoundarySegment segment in list3)
					{
						Element element = segment.GetElement(this.m_doc);
						if (element != null && element is Wall)
						{
							list2.Add((element as Wall).Id.IntegerValue);
						}
					}
				}
			}
			list2 = list2.Distinct<int>().ToList<int>();
			foreach (int num in list2)
			{
				ElementId elementId = new ElementId(num);
				list.Add(this.m_doc.GetElement(elementId) as Wall);
			}
			return list;
		}

		public List<Dictionary<Wall, Curve>> GetWallByCurve(List<Wall> lstWalls, CurveArray aryCurve)
		{
			List<Dictionary<Wall, Curve>> list = new List<Dictionary<Wall, Curve>>();
			foreach (Wall wall in lstWalls)
			{
				foreach (object obj in aryCurve)
				{
					Curve curve = (Curve)obj;
					if (this.IsCurveInWall(curve, wall))
					{
						list.Add(new Dictionary<Wall, Curve>
						{
							{
								wall,
								curve
							}
						});
					}
				}
			}
			for (int i = 0; i < list.Count; i++)
			{
				foreach (KeyValuePair<Wall, Curve> keyValuePair in list[i])
				{
					if (keyValuePair.Value is Line)
					{
						YJKLine hyline = new YJKLine(keyValuePair.Value as Line);
						Curve curve2 = (keyValuePair.Key.Location as LocationCurve).Curve;
						if (!hyline.IsParallel(curve2 as Line))
						{
							list.Remove(list[i]);
							i--;
						}
					}
				}
			}
			return list;
		}

		public List<Dictionary<Wall, Curve>> GetWallByCurve(List<CurveInformationBean> lstInputCurveInformationBean)
		{
			List<Dictionary<Wall, Curve>> list = new List<Dictionary<Wall, Curve>>();
			IEnumerable<CurveInformationBean> enumerable = from n in lstInputCurveInformationBean
			where n.ThisElement != null && n.ThisElement is Wall
			select n;
			if (enumerable.Count<CurveInformationBean>() > 0)
			{
				foreach (CurveInformationBean curveInformationBean in enumerable)
				{
					list.Add(new Dictionary<Wall, Curve>
					{
						{
							curveInformationBean.ThisElement as Wall,
							curveInformationBean.ThisCurve
						}
					});
				}
			}
			return list;
		}

		public List<Curve> DeleteRepeatLines(List<Curve> lines)
		{
			List<Curve> list = new List<Curve>();
			for (int i = 0; i < lines.Count; i++)
			{
				Curve curve = lines[i];
				for (int j = 0; j < lines.Count; j++)
				{
					if (i != j && this.sureEqule(lines[j] as Line, lines[i] as Line))
					{
						if (lines[j].Length < lines[i].Length)
						{
							lines.RemoveAt(j);
							j--;
						}
						else
						{
							lines.RemoveAt(i);
							if (i < j)
							{
								j--;
							}
						}
					}
				}
			}
			return lines;
		}

		public List<List<Curve>> GetCurveAryForBuildingArea(List<List<CurveInformationBean>> areas, List<Room> rooms, string RangeType, List<Wall> lstInputWalls)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			new List<List<Curve>>();
			new List<Pole2D>();
			new CurveArray();
			Common.m_Revit = this.m_Revit;
			this.GetSelectWalls(rooms);
			List<Curve> lstTempAppendCurves = new List<Curve>();
			for (int i = 0; i < areas.Count; i++)
			{
				lstTempAppendCurves = new List<Curve>();
				List<Dictionary<Wall, Curve>> wallByCurve = this.GetWallByCurve(areas[i]);
				lstTempAppendCurves = (from n in areas[i]
				where n.ThisElement == null || !(n.ThisElement is Wall)
				select n.ThisCurve).ToList<Curve>();
				using (List<List<Curve>>.Enumerator enumerator = this.GetOffsetCurveForBuildingArea(wallByCurve, lstInputWalls, lstTempAppendCurves, RangeType).GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						List<Curve> item = enumerator.Current;
						list.Add(item);
					}
				}
			}
			return list;
		}

		public List<List<Curve>> GetCurveAryForInsideArea(List<List<CurveInformationBean>> areas, List<Room> rooms, List<Wall> lstAllOutWall, string RangeType)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			new List<List<Curve>>();
			new List<Pole2D>();
			new CurveArray();
			Common.m_Revit = this.m_Revit;
			this.GetSelectWalls(rooms);
			List<Curve> lstTempAppendCurves = new List<Curve>();
			IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_RoomSeparationLines).ToElements().ToList<Element>();
			List<Curve> list2 = new List<Curve>();
			foreach (Element element in enumerable)
			{
				if (element != null)
				{
					list2.Add(((element as ModelCurve).Location as LocationCurve).Curve);
				}
			}
			for (int i = 0; i < areas.Count; i++)
			{
				lstTempAppendCurves = new List<Curve>();
				List<Dictionary<Wall, Curve>> wallByCurve = this.GetWallByCurve(areas[i]);
				lstTempAppendCurves = (from n in areas[i]
				where n.ThisElement == null || !(n.ThisElement is Wall)
				select n.ThisCurve).ToList<Curve>();
				using (List<List<Curve>>.Enumerator enumerator2 = this.GetOffsetCurveForInsideArea(wallByCurve, lstAllOutWall, lstTempAppendCurves, list2, RangeType).GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						List<Curve> item = enumerator2.Current;
						list.Add(item);
					}
				}
			}
			return list;
		}

		public List<Curve> GetCurveAryForFireZoneArea(ClosedArea closedArea, List<Room> rooms)
		{
			new List<List<Curve>>();
			new List<Pole2D>();
			CurveArray curveArray = new CurveArray();
			List<Wall> outWallsCurViewPlan = this.GetOutWallsCurViewPlan(this.m_doc.ActiveView as Autodesk.Revit.DB.ViewPlan, this.m_doc);
			List<Wall> selectWalls = this.GetSelectWalls(rooms);
			foreach (Curve curve in closedArea.m_outerBoundary)
			{
				curveArray.Append(curve);
			}
			List<Curve> lstTempAppendCurves = new List<Curve>();
			List<Dictionary<Wall, Curve>> wallByCurve;
			if (closedArea is ClosedAreaMoreInformation)
			{
				List<CurveInformationBean> list = new List<CurveInformationBean>();
				foreach (CurveInformationBean item in (closedArea as ClosedAreaMoreInformation).LstOuterBoundaryBean)
				{
					list.Add(item);
				}
				wallByCurve = this.GetWallByCurve(list);
				lstTempAppendCurves = (from n in list
				where n.ThisElement == null || !(n.ThisElement is Wall)
				select n.ThisCurve).ToList<Curve>();
			}
			else
			{
				wallByCurve = this.GetWallByCurve(selectWalls, curveArray);
			}
			return this.GetOffsetCurveForFireArea(wallByCurve, outWallsCurViewPlan, lstTempAppendCurves, Common.m_WallRangeType)[0];
		}

		private List<List<Curve>> GetOffsetCurveForBuildingArea(List<Dictionary<Wall, Curve>> dicWall2Curve, List<Wall> lstAllOutWall, List<Curve> lstTempAppendCurves, string RangeType)
		{
			List<int> list = new List<int>();
			new List<List<Curve>>();
			List<KeyValuePair<Curve, double>> edgeLoop = new List<KeyValuePair<Curve, double>>();
			foreach (Wall wall in lstAllOutWall)
			{
				list.Add(wall.Id.IntegerValue);
			}
			edgeLoop = this.GetEachCurveOffsetVal(dicWall2Curve, list, lstTempAppendCurves, null, RangeType);
			return new StructureLoopSearcher(this.m_Revit).OffsetLoop(edgeLoop, true, true);
		}

		private List<List<Curve>> GetOffsetCurveForInsideArea(List<Dictionary<Wall, Curve>> dicWall2Curve, List<Wall> lstAllOutWall, List<Curve> lstTempAppendCurves, List<Curve> lstRoomSeparationcv, string RangeType)
		{
			List<int> list = new List<int>();
			new List<List<Curve>>();
			List<KeyValuePair<Curve, double>> edgeLoop = new List<KeyValuePair<Curve, double>>();
			foreach (Wall wall in lstAllOutWall)
			{
				list.Add(wall.Id.IntegerValue);
			}
			edgeLoop = this.GetEachCurveOffsetVal(dicWall2Curve, list, lstTempAppendCurves, lstRoomSeparationcv, RangeType);
			return new StructureLoopSearcher(this.m_Revit, 0.001).OffsetLoop(edgeLoop, true, false);
		}

		private List<List<Curve>> GetOffsetCurveForFireArea(List<Dictionary<Wall, Curve>> dicWall2Curve, List<Wall> lstAllOutWall, List<Curve> lstTempAppendCurves, string RangeType)
		{
			List<int> list = new List<int>();
			new List<List<Curve>>();
			List<KeyValuePair<Curve, double>> edgeLoop = new List<KeyValuePair<Curve, double>>();
			foreach (Wall wall in lstAllOutWall)
			{
				list.Add(wall.Id.IntegerValue);
			}
			edgeLoop = this.GetEachCurveOffsetVal(dicWall2Curve, list, lstTempAppendCurves, null, RangeType);
			double dEps = 0.01;
			return new StructureLoopSearcher(this.m_Revit, dEps).OffsetLoop(edgeLoop, true, true);
		}

		private List<KeyValuePair<Curve, double>> GetEachCurveOffsetVal(List<Dictionary<Wall, Curve>> lstOutWallCurve, List<int> lstOutWallId, List<Curve> lstTempAppendCurves, List<Curve> lstRoomSeparationcv, string RangeType)
		{
			List<Curve> list = new List<Curve>();
			List<KeyValuePair<Curve, double>> list2 = new List<KeyValuePair<Curve, double>>();
			Curve key = null;
			bool flag = false;
			Wall wall = null;
			foreach (Dictionary<Wall, Curve> dictionary in lstOutWallCurve)
			{
				if (dictionary != null && dictionary.First<KeyValuePair<Wall, Curve>>().Key != null && !(dictionary.First<KeyValuePair<Wall, Curve>>().Value == null))
				{
					list.Add(dictionary.First<KeyValuePair<Wall, Curve>>().Value);
					wall = dictionary.First<KeyValuePair<Wall, Curve>>().Key;
					key = dictionary.First<KeyValuePair<Wall, Curve>>().Value;
					double num = 0.0;
					double num2 = 0.0;
					KeyValuePair<Curve, double> item = default(KeyValuePair<Curve, double>);
					IList<CompoundStructureLayer> layers = wall.WallType.GetCompoundStructure().GetLayers();
					if (RangeType.Equals("Structure"))
					{
						IEnumerator<CompoundStructureLayer> enumerator2 = layers.GetEnumerator();
						{
							while (enumerator2.MoveNext())
							{
								CompoundStructureLayer compoundStructureLayer = enumerator2.Current;
								if ((int)compoundStructureLayer.Function == 1)
								{
									break;
								}
								num += compoundStructureLayer.Width;
							}
							goto IL_1EF;
						}
						goto IL_121;
					}
					goto IL_121;
					IL_1EF:
					if (lstOutWallId.Contains(wall.Id.IntegerValue))
					{
						if (this.m_strLineLocation == "OutLine")
						{
							item = new KeyValuePair<Curve, double>(key, num);
						}
						else
						{
							item = new KeyValuePair<Curve, double>(key, wall.Width / 2.0 - num);
						}
					}
					else
					{
						item = new KeyValuePair<Curve, double>(key, 0.0);
					}
					list2.Add(item);
					continue;
					IL_121:
					if (RangeType.Equals("KeepWarm"))
					{
						foreach (CompoundStructureLayer compoundStructureLayer2 in layers)
						{
							if ((int)compoundStructureLayer2.Function == 3)
							{
								flag = true;
								break;
							}
							num += compoundStructureLayer2.Width;
						}
						if (!flag)
						{
							num = 0.0;
							goto IL_1EF;
						}
						goto IL_1EF;
					}
					else
					{
						if (RangeType.Equals("StrCenter"))
						{
							foreach (CompoundStructureLayer compoundStructureLayer3 in layers)
							{
								num += compoundStructureLayer3.Width;
								if ((int)compoundStructureLayer3.Function == 1)
								{
									num2 = compoundStructureLayer3.Width / 2.0;
									break;
								}
							}
							num -= num2;
							goto IL_1EF;
						}
						goto IL_1EF;
					}
				}
			}
			KeyValuePair<Curve, double> item2 = default(KeyValuePair<Curve, double>);
			foreach (Curve key2 in lstTempAppendCurves)
			{
				item2 = new KeyValuePair<Curve, double>(key2, 0.0);
				list2.Add(item2);
			}
			list.AddRange(lstTempAppendCurves);
			if (lstRoomSeparationcv != null && this.GetAreacvEqualRoomcv(lstRoomSeparationcv, lstTempAppendCurves))
			{
				foreach (Curve key3 in this.AddCurveToUseCurves(list))
				{
					item2 = new KeyValuePair<Curve, double>(key3, 0.0);
					list2.Add(item2);
				}
			}
			return list2;
		}

		public bool GetAreacvEqualRoomcv(List<Curve> inputAreaCurve, List<Curve> inputRoomSeparationCv)
		{
			foreach (Curve c in inputAreaCurve)
			{
				foreach (Curve c2 in inputRoomSeparationCv)
				{
					if (OverlapType.eOverlap == CurveAssistFunc.GetOverlapType(this.m_Revit.Application, c, c2))
					{
						return true;
					}
				}
			}
			return false;
		}

		public List<Curve> GetCurveOfColumn(List<CurveInformationBean> inputCurveBean)
		{
			List<Curve> list = new List<Curve>();
			foreach (CurveInformationBean curveInformationBean in from n in inputCurveBean
			where n.ThisElement == null || !(n.ThisElement is Wall)
			select n)
			{
				list.Add(curveInformationBean.ThisCurve);
			}
			return list;
		}

		private void AddSolids(GeometryElement geomElem, ref Solid solids)
		{
			foreach (GeometryObject geometryObject in geomElem)
			{
				Solid solid = geometryObject as Solid;
				if (null != solid)
				{
					solids = solid;
				}
				else
				{
					GeometryInstance geometryInstance = geometryObject as GeometryInstance;
					if (null != geometryInstance)
					{
						GeometryElement instanceGeometry = geometryInstance.GetInstanceGeometry(geometryInstance.Transform);
						this.AddSolids(instanceGeometry, ref solids);
					}
				}
			}
		}

		private ViewPlan GetViewPlanFromName(string strViewPlanName, Autodesk.Revit.DB.Document doc)
		{
			foreach (Autodesk.Revit.DB.ViewPlan viewPlan in FloorSelectManager.GetAllFireAreaViewPlan(doc))
			{
				if (viewPlan.Name == strViewPlanName)
				{
					return viewPlan;
				}
			}
			return null;
		}

		private bool IsCurveInWall(Curve tmpcurve, Wall tmpwall)
		{
			bool result = false;
			if (tmpcurve == null || tmpwall == null)
			{
				return result;
			}
			double num = (tmpwall.Width + 0.2) / 2.0;
			if (tmpcurve is Line)
			{
				new YJKLine(tmpcurve as Line);
			}
			Curve curve = (tmpwall.Location as LocationCurve).Curve;
			double z = curve.GetEndPoint(0).Z;
			Curve curve2 = CurveUtility.CreateOffsetCurve(curve, num);
			Curve curve3 = CurveUtility.CreateOffsetCurve(curve, -num);
			Line item = Line.CreateBound(curve2.GetEndPoint(0), curve3.GetEndPoint(0));
			Line item2 = Line.CreateBound(curve3.GetEndPoint(1), curve2.GetEndPoint(1));
			List<Curve> list = new List<Curve>();
			list.Add(curve2);
			list.Add(item);
			list.Add(curve3.CreateReversed());
			list.Add(item2);
			if (tmpcurve is Line && curve is Line)
			{
				XYZ xyz = Geometry.CalculatMidPoint(tmpcurve as Line);
				XYZ pt = new XYZ(xyz.X, xyz.Y, z);
				result = CurveUtility.IsPointInArea(list, pt);
			}
			else if (tmpcurve is Arc)
			{
				XYZ xyz2 = Geometry.CalculatMidPoint(tmpcurve as Arc);
				XYZ pt2 = new XYZ(xyz2.X, xyz2.Y, z);
				result = CurveUtility.IsPointInArea(list, pt2);
			}
			return result;
		}

		private bool sureEqule(Line line1, Line line2)
		{
			return (line1.GetEndPoint(0).DistanceTo(line2.GetEndPoint(0)) < 1E-06 && line1.GetEndPoint(1).DistanceTo(line2.GetEndPoint(1)) < 1E-06) || (line1.GetEndPoint(0).DistanceTo(line2.GetEndPoint(1)) < 1E-06 && line1.GetEndPoint(1).DistanceTo(line2.GetEndPoint(0)) < 1E-06);
		}

		private List<Curve> FindLinkeCurves(List<XYZ> inputPoints)
		{
			List<Curve> list = new List<Curve>();
			for (int i = 0; i < inputPoints.Count; i++)
			{
				XYZ xyz = inputPoints[i];
				XYZ xyz2 = null;
				double num = double.MaxValue;
				for (int j = 0; j < inputPoints.Count; j++)
				{
					if (i != j)
					{
						XYZ xyz3 = inputPoints[j];
						if (xyz.DistanceTo(xyz3) < num)
						{
							xyz2 = xyz3;
							num = xyz.DistanceTo(xyz3);
						}
					}
				}
				if (xyz2 != null && !Geometry.IsEqual(xyz, xyz2))
				{
					list.Add(Line.CreateBound(xyz, xyz2));
				}
			}
			return list;
		}

		private Pole2D GetWallInCurve(KeyValuePair<Wall, Curve> kvp, List<int> lstOutWallId)
		{
			Pole2D result = null;
			if (this.m_strLineLocation == "OutLine")
			{
				if (kvp.Value is Line)
				{
					result = Pole2D.TransformWallToStructure2DPole(kvp.Value, kvp.Key.Id, 0.0, kvp.Key.Width);
				}
				else if (kvp.Value is Arc && lstOutWallId.Contains(kvp.Key.Id.IntegerValue))
				{
					result = Pole2D.TransformWallToStructure2DPole(kvp.Value, kvp.Key.Id, kvp.Key.Width, 0.0);
				}
				else if (kvp.Value is Arc && this.JudgeWallIsOutOrIn(kvp.Key))
				{
					result = Pole2D.TransformWallToStructure2DPole(kvp.Value, kvp.Key.Id, 0.0, kvp.Key.Width);
				}
			}
			else if (this.m_strLineLocation == "CenterLine")
			{
				if (kvp.Value is Line)
				{
					result = Pole2D.TransformWallToStructure2DPole(kvp.Value, kvp.Key.Id, kvp.Key.Width / 2.0, 0.0);
				}
				else if (kvp.Value is Arc && lstOutWallId.Contains(kvp.Key.Id.IntegerValue))
				{
					result = Pole2D.TransformWallToStructure2DPole(kvp.Value, kvp.Key.Id, 0.0, kvp.Key.Width / 2.0);
				}
				else if (kvp.Value is Arc && this.JudgeWallIsOutOrIn(kvp.Key))
				{
					result = Pole2D.TransformWallToStructure2DPole(kvp.Value, kvp.Key.Id, kvp.Key.Width / 2.0, 0.0);
				}
			}
			return result;
		}

		private List<Document> GetLinkDocs(Autodesk.Revit.DB.Document doc)
		{
			List<Document> list = new List<Document>();
			try
			{
				foreach (RevitLinkInstance revitLinkInstance in ((IEnumerable<RevitLinkInstance>)new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)).Cast<RevitLinkInstance>().ToList<RevitLinkInstance>()))
				{
					Document linkDocument = revitLinkInstance.GetLinkDocument();
					if (linkDocument != null && !string.IsNullOrEmpty(linkDocument.PathName))
					{
						list.Add(linkDocument);
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return list;
		}

		private List<Pole2D> OffsetCurveByRangeType(List<Pole2D> lstOutWallCurve, string RangeType, List<int> lstAllOutWall)
		{
			List<Pole2D> list = new List<Pole2D>();
			Curve curve = null;
			bool flag = false;
			Wall wall = null;
			List<Document> linkDocs = this.GetLinkDocs(this.m_doc);
			foreach (Pole2D pole2D in lstOutWallCurve)
			{
				if (pole2D != null)
				{
					if (this.m_strLineLocation == "OutLine")
					{
						curve = pole2D.PositiveCurve();
					}
					else if (this.m_strLineLocation == "CenterLine")
					{
						curve = pole2D.NegativeCurve();
					}
					wall = (this.m_doc.GetElement(pole2D.ComponentId) as Wall);
					if (curve == null || wall == null)
					{
						foreach (Autodesk.Revit.DB.Document document in linkDocs)
						{
							wall = (document.GetElement(pole2D.ComponentId) as Wall);
							if (wall != null)
							{
								break;
							}
						}
					}
					if (!(curve == null) && wall != null)
					{
						double num = 0.0;
						double num2 = 0.0;
						Pole2D item = null;
						IList<CompoundStructureLayer> layers = wall.WallType.GetCompoundStructure().GetLayers();
						if (RangeType.Equals("Structure"))
						{
							IEnumerator<CompoundStructureLayer> enumerator3 = layers.GetEnumerator();
							{
								while (enumerator3.MoveNext())
								{
									CompoundStructureLayer compoundStructureLayer = enumerator3.Current;
									if ((int)compoundStructureLayer.Function == 1)
									{
										break;
									}
									num += compoundStructureLayer.Width;
								}
								goto IL_232;
							}
							goto IL_166;
						}
						goto IL_166;
						IL_232:
						if (lstAllOutWall.Contains(wall.Id.IntegerValue))
						{
							if (this.m_strLineLocation == "OutLine")
							{
								item = Pole2D.TransformWallToStructure2DPole(curve, wall.Id, wall.Width - num, 0.0);
							}
							else if (this.m_strLineLocation == "CenterLine")
							{
								item = Pole2D.TransformWallToStructure2DPole(curve, wall.Id, 0.0, wall.Width - num);
							}
						}
						else if (this.JudgeWallIsOutOrIn(wall))
						{
							item = Pole2D.TransformWallToStructure2DPole(curve, wall.Id, num, 0.0);
						}
						else if (this.m_strLineLocation == "OutLine")
						{
							item = Pole2D.TransformWallToStructure2DPole(curve, wall.Id, wall.Width, 0.0);
						}
						else if (this.m_strLineLocation == "CenterLine")
						{
							item = Pole2D.TransformWallToStructure2DPole(curve, wall.Id, 0.0, wall.Width / 2.0);
						}
						list.Add(item);
						continue;
						IL_166:
						if (RangeType.Equals("KeepWarm"))
						{
							foreach (CompoundStructureLayer compoundStructureLayer2 in layers)
							{
								if ((int)compoundStructureLayer2.Function == 3)
								{
									flag = true;
									break;
								}
								num += compoundStructureLayer2.Width;
							}
							if (!flag)
							{
								num = 0.0;
								goto IL_232;
							}
							goto IL_232;
						}
						else
						{
							if (RangeType.Equals("StrCenter"))
							{
								foreach (CompoundStructureLayer compoundStructureLayer3 in layers)
								{
									num += compoundStructureLayer3.Width;
									if ((int)compoundStructureLayer3.Function == 1)
									{
										num2 = compoundStructureLayer3.Width / 2.0;
										break;
									}
								}
								num -= num2;
								goto IL_232;
							}
							goto IL_232;
						}
					}
				}
			}
			return list;
		}

		private List<Room> FilterRooms(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			List<Room> list = new List<Room>();
			filteredElementCollector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_Rooms);
			foreach (Element element in filteredElementCollector)
			{
				Room room = element as Room;
				if (room != null)
				{
					list.Add(room);
				}
			}
			return list;
		}

		private bool JudgeWallIsOutOrIn(Wall wall)
		{
			List<int> list = new List<int>();
			new List<Room>();
			int num = 0;
			foreach (Room room in this.FilterRooms(this.m_doc))
			{
				foreach (IList<BoundarySegment> list2 in room.GetBoundarySegments(new SpatialElementBoundaryOptions()))
				{
					foreach (BoundarySegment segment in list2)
					{
						if (segment.GetElement(this.m_doc) != null && segment.GetElement(this.m_doc) is Wall)
						{
							Wall wall2 = segment.GetElement(this.m_doc) as Wall;
							list.Add(wall2.Id.IntegerValue);
						}
					}
				}
			}
			using (List<int>.Enumerator enumerator4 = list.GetEnumerator())
			{
				while (enumerator4.MoveNext())
				{
					if (enumerator4.Current == wall.Id.IntegerValue)
					{
						num++;
					}
				}
			}
			return num <= 1;
		}

		private Curve ReplaceTwoCurves(Curve oneCurve, Curve twoCurve)
		{
			Curve result = null;
			XYZ xyz = new YJKLine(oneCurve as Line).IntersectionPoint(twoCurve as Line);
			List<XYZ> list = new List<XYZ>();
			list.Add(oneCurve.GetEndPoint(0));
			list.Add(oneCurve.GetEndPoint(1));
			list.Add(twoCurve.GetEndPoint(0));
			list.Add(twoCurve.GetEndPoint(1));
			for (int i = 0; i < list.Count; i++)
			{
				if (xyz != null && list[i].IsAlmostEqualTo(xyz))
				{
					list.Remove(list[i]);
					i--;
				}
			}
			if (list.Count != 0)
			{
				result = Line.CreateBound(list[0], list[1]);
			}
			return result;
		}

		private Autodesk.Revit.DB.Document m_doc;

		private ExternalCommandData m_Revit;

		private string m_strLineLocation;

		private bool m_bIsReverseMigration;
	}
}
